var app = getApp();
import { CategoryModel } from '../../models/CategoryModel.js'
let Adress = new CategoryModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //收货地址数据
    addressData: [],
    tag: ['家', '公司','学校','其他'],
    len:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.address_check()
  },
  //从new-address返回更新本页面
  go_update(){
    this.address_check()
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // var that = this
    // // 查询购物车地址
    // that.onLoad()
  },
  // 查询购物车地址
  address_check:function(){
    wx.showLoading({
      title: '加载中...',
    })
    var that = this
    Adress.getAdress(res=>{
      console.log(res)
      wx.hideLoading()
      let data = res.result.data.data
      for (var i = 0; i < data.length; i++) {
        if(data[i].default == '1'){
          let obj = data[i];
          data.splice(i, 1);
          data.unshift(obj);
          break
        }
      }
      that.setData({
        addressData: data
      })
    })
  },
  // 删除购物车地址
  removeAddress:function(e){
    let that = this
    console.log(e)
    let id = e.currentTarget.dataset.id
    wx.showModal({
      title: '是否删除选中的地址?',
      content: '',
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#000000',
      confirmText: '确定',
      confirmColor: '#3CC51F',
      success: async(result) => {
          if (result.confirm) {
            that.removeAddress_r(e,id);
          }
      }
    });
  },
  removeAddress_r:function(e,id){
    let that = this
    //删除购物车商品
    Adress.removeAddress(id, data => {
      console.log(data)
      if(data.errMsg == "cloud.callFunction:ok"){
        that.data.addressData.splice(e.currentTarget.dataset.index, 1);
        that.setData({
          addressData: that.data.addressData
        })
        wx.showToast({
          title: '删除成功',
          icon: 'none',
          duration: 1000
        })
      }
    })
  },
  // 新增购物车地址
  addAddess: function() {
    let ids = ''
    let data = this.data.addressData
    data.forEach(item => {
        if(item.default == 1){
          ids = item._id
        }
    });
    wx.navigateTo({
      url: "../new-address/new-address?_id=" + ids
    })
  },
  //修改收货地址
  updateAddress: function (e) {
    console.log(e)
    let data = this.data.addressData
    let id = e.currentTarget.dataset.id
    let ids = ''
    data.forEach(item => {
        if(item.default == 1){
          ids = item._id
        }
    });
    const address = data.find(ele => {
      return ele._id == id
    })
    let res = {id,ids}
    wx.setStorageSync('address',address)
    wx.navigateTo({
      url: '../new-address/new-address?result=' + JSON.stringify(res)
    })
  },
  //选择收货地址
  selectAddress: function (e) {
    let index = e.currentTarget.dataset.index;
    let address = this.data.addressData[index];
    
    app.globalData.address = true
    address.index = index
      wx.setStorageSync('address', address)
      console.log(address) 
      wx.navigateBack({
        delta: 1
      });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})